package com.zqxx.examine.management.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zqxx.examine.common.utils.Page;
import com.zqxx.examine.common.utils.StringUtil;
import com.zqxx.examine.management.persistence.ExamMapper;
import com.zqxx.examine.management.persistence.ExamPaperMapper;
import com.zqxx.examine.management.persistence.UserMapper;
import com.zqxx.examine.model.exam.AnswerSheet;
import com.zqxx.examine.model.exam.Exam;
import com.zqxx.examine.model.exam.ExamHistory;
import com.zqxx.examine.model.exam.ExamPaper;
import com.zqxx.examine.model.user.Role;
import com.zqxx.examine.model.user.User;
@Service
public class ExamService {
	@Autowired
	private ExamMapper examMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private ExamPaperMapper examPaperMapper;

	@Transactional
	public void addExam(Exam exam) {
		try {
			this.examMapper.addExam(exam);
			if ((exam.getGroupIdList() != null) && (exam.getGroupIdList().size() > 0)) {
				List<User> userList = this.userMapper.getUserListByGroupIdList(exam.getGroupIdList(), null);
				ExamPaper examPaper = this.examPaperMapper.getExamPaperById(exam.getExamPaperId());
				SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
				Date now = new Date();
				for (User user : userList) {
					ExamHistory history = new ExamHistory();
					history.setExamId(exam.getExamId());
					history.setExamPaperId(exam.getExamPaperId());
					history.setContent(examPaper.getContent());
					history.setDuration(examPaper.getDuration());

					history.setApproved(1);

					String seriNo = sdf.format(now) + StringUtil.format(user.getUserId(), 3)
							+ StringUtil.format(exam.getExamId(), 3);
					history.setSeriNo(seriNo);
					history.setVerifyTime(new Date());
					history.setUserId(user.getUserId());
					this.examMapper.addUserExamHist(history);
				}
			}
		} catch (Exception e) {
			ExamPaper examPaper;
			SimpleDateFormat sdf;
			Date now;
			throw new RuntimeException(e);
		}
	}

	@Transactional
	public void addExamUser(int examId, String userNameStr, HashMap<String, Role> roleMap) {
		try {
			String[] userNames = userNameStr.split(";");
			List<User> userList = this.userMapper.getUserByNames(userNames,
					((Role) roleMap.get("ROLE_STUDENT")).getRoleId());
			Exam exam = this.examMapper.getExamById(examId);
			ExamPaper 	examPaper = this.examPaperMapper.getExamPaperById(exam.getExamPaperId());
			SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
			Date now = new Date();
			for (User user : userList) {
				ExamHistory history = getUserExamHistByUserIdAndExamId(user.getUserId(), examId,
						new int[] { 0, 1, 2, 3 });
				if (history == null) {
					history = new ExamHistory();
					history.setExamId(exam.getExamId());
					history.setExamPaperId(exam.getExamPaperId());
					history.setContent(examPaper.getContent());
					history.setDuration(examPaper.getDuration());

					history.setApproved(1);
					String seriNo = sdf.format(now) + StringUtil.format(user.getUserId(), 3)
							+ StringUtil.format(exam.getExamId(), 3);
					history.setSeriNo(seriNo);
					history.setVerifyTime(new Date());
					history.setUserId(user.getUserId());
					this.examMapper.addUserExamHist(history);
				} else if (history.getApproved() == 0) {
					this.examMapper.deleteUserExamHistByUserId(exam.getExamId(), user.getUserId());

					history.setApproved(1);
					this.examMapper.addUserExamHist(history);
				}
			}
		} catch (Exception e) {
			Exam exam;
			ExamPaper examPaper;
			SimpleDateFormat sdf;
			Date now;
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public List<Exam> getExamList(Page<Exam> page, int... typeIdList) {
		if (typeIdList.length == 0) {
			typeIdList = null;
		}
		return this.examMapper.getExamList(typeIdList, page);
	}

	public List<ExamHistory> getUserExamHistListByExamId(int examId, String searchStr, String order, int limit,
			Page<ExamHistory> page) {
		return this.examMapper.getUserExamHistListByExamId(examId, searchStr, order, limit, page);
	}

	public void deleteExamById(int examId) throws Exception {
		Exam exam = this.examMapper.getExamById(examId);
		if ((exam.getApproved() == 0) || (exam.getApproved() == 2)) {
			this.examMapper.deleteExamById(examId);
		} else {
			throw new Exception("考试已经审核通过！不允许删除！");
		}
	}

	public void changeExamStatus(int examId, int approved) {
		this.examMapper.changeExamStatus(examId, approved);
	}

	public void changeUserExamHistStatus(int histId, int approved) {
		this.examMapper.changeUserExamHistStatus(histId, approved);
	}

	public void updateUserExamHist(AnswerSheet answerSheet, String answerSheetStr, int approved) {
		this.examMapper.updateUserExamHist(answerSheet, answerSheetStr, approved);
	}

	public ExamHistory getUserExamHistListByHistId(int histId) {
		return this.examMapper.getUserExamHistListByHistId(histId);
	}

	public void deleteUserExamHist(int histId) {
		this.examMapper.deleteUserExamHist(histId);
	}

	public Exam getExamById(int examId) {
		return this.examMapper.getExamById(examId);
	}

	public ExamHistory getUserExamHistByUserIdAndExamId(int userId, int examId, int... approved) {
		if (approved.length == 0) {
			approved = null;
		}
		return this.examMapper.getUserExamHistByUserIdAndExamId(userId, examId, approved);
	}

	@Transactional
	public void addGroupUser2Exam(List<Integer> groupIdList, int examId) {
		try {
			Exam exam = this.examMapper.getExamById(examId);
			ExamPaper examPaper = this.examPaperMapper.getExamPaperById(exam.getExamPaperId());
			List<User> userList = this.userMapper.getUserListByGroupIdList(groupIdList, null);
			SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
			Date now = new Date();
			for (User user : userList) {
				ExamHistory history = getUserExamHistByUserIdAndExamId(user.getUserId(), examId,
						new int[] { 0, 1, 2, 3 });
				if (history == null) {
					history = new ExamHistory();
					history.setExamId(exam.getExamId());
					history.setExamPaperId(exam.getExamPaperId());
					history.setContent(examPaper.getContent());
					history.setDuration(examPaper.getDuration());

					history.setApproved(1);
					String seriNo = sdf.format(now) + StringUtil.format(user.getUserId(), 3)
							+ StringUtil.format(exam.getExamId(), 3);
					history.setSeriNo(seriNo);
					history.setVerifyTime(new Date());
					history.setUserId(user.getUserId());
					this.examMapper.addUserExamHist(history);
				} else if (history.getApproved() == 0) {
					this.examMapper.deleteUserExamHistByUserId(exam.getExamId(), user.getUserId());

					history.setApproved(1);
					this.examMapper.addUserExamHist(history);
				}
			}
		} catch (Exception e) {
			Exam exam;
			ExamPaper examPaper;
			SimpleDateFormat sdf;
			Date now;
			throw new RuntimeException(e);
		}
	}

	public List<ExamHistory> getUserExamHistList(Page<ExamHistory> page, int... approved) {
		if (approved.length == 0) {
			approved = null;
		}
		return this.examMapper.getUserExamHistList(approved, page);
	}
}
